package com.woniu.account.consumer;



import com.woniu.account.util.SMSUtil1;
import com.woniu.model.RedisKey;
import com.woniu.util.RedisUtil;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@RabbitListener(queues = "register_phone_queue")
public class RegisterCodeConsumer {

    @Resource
    private SMSUtil1 smsUtil1;
    @Resource
    private RedisUtil redisUtil;


    @RabbitHandler
    public void sendCode(String mobile) throws Exception {
        //调用第三方的短信服务发送验证码
        Integer code = (int) (Math.random() * 900000) + 100000;
        String result = smsUtil1.sendCode(mobile, code + "");

        //发送成功
        System.out.println(result);
        if (result.equals("2")) {
            //将验证码保存到Redis
            redisUtil.set(RedisKey.registerCodeKey(mobile), code + "", 5 * 60);
            //保存一个过期时间1分钟的键值对到Redis
            redisUtil.set(RedisKey.registerCodeOnMinute(mobile), "", 60);
            //重置错误次数
            redisUtil.delete(RedisKey.codeErrorNumber(mobile));
            if (!redisUtil.exist(RedisKey.sendMessageNumber(mobile))) {
                redisUtil.set(RedisKey.sendMessageNumber(mobile), "1", 60 * 60);
            } else {
                redisUtil.incr(RedisKey.sendMessageNumber(mobile));
            }
        }
    }





}
